using System;
using System.Collections.Generic;
using OracleInternal.Common;
using OracleInternal.ConnectionPool;
using OracleInternal.I18N;
using OracleInternal.NotificationServices;

namespace OracleInternal.ServiceObjects
{
	internal class OracleRLBNotificationManager : OracleONSNotificationManager
	{
		private string m_serviceName = string.Empty;

		protected override string GetEventTypeForNotification(string eventTypeFromDb, string serviceName)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.GetEventTypeForNotification);
			}
			try
			{
				string text = "(\"eventType=database/event/servicemetrics/" + serviceName + "\")";
				string arg = "dynamic";
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.SvcObj, OracleTraceClassName.OracleONSNotificationManager, OracleTraceFuncName.RegisterForNotification, $"(RLB) (string.type={arg}) (subscription.string={text})");
				}
				return text;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.GetEventTypeForNotification);
				}
			}
		}

		protected override void HandleEvent(object notification)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent);
			}
			try
			{
				if (notification == null)
				{
					return;
				}
				try
				{
					byte[] array = Array.ConvertAll(((Notification)notification).body(), (sbyte a) => (byte)a);
					string text = Conv.GetInstance(871).ConvertBytesToString(array, 0, array.Length);
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, OracleTraceTag.RLB, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent, text);
					}
					RLB rLB = RLBManager.Put(text);
					if (rLB == null)
					{
						return;
					}
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, OracleTraceTag.RLB, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent, "RLB data available. Lets try and use it to update the service member instance DOWN names");
					}
					List<OraclePoolManager> list = OracleConnectionDispenser<OraclePoolManager, OraclePool, OracleConnectionImpl>.m_listPM.GetList();
					for (int i = 0; i < list.Count; i++)
					{
						OraclePoolManager oraclePoolManager = list[i];
						ServiceCtx serviceCtx = null;
						if (rLB.m_service != null)
						{
							serviceCtx = oraclePoolManager.m_dictSvcCtx[rLB.m_service.ToLowerInvariant()];
						}
						if (serviceCtx != null)
						{
							serviceCtx.CheckAndUpdateServiceMemberDOWNNames_RLB(rLB);
						}
						else if (ProviderConfig.m_bTraceLevelPrivate)
						{
							Trace.Write(OracleTraceLevel.Private, OracleTraceTag.RLB, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent, string.Format("ServiceCtx does not exist for service {0} in PM {1}", (rLB.m_service != null) ? rLB.m_service : "null", oraclePoolManager.m_id));
						}
					}
				}
				catch (Exception ex)
				{
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent, "OracleRLBNotificationManager::HandleEvent() failed. -" + ex.Message);
					}
				}
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleRLBNotificationManager, OracleTraceFuncName.HandleEvent);
				}
			}
		}
	}
}
